﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using DALEntities;

namespace MapEntities
{



    [Serializable]
    public class SymbolItemVisualization
    {
        private SymbolItemRangeList rangeList = null;
        public SymbolType DefaultSymbolType { get; set; }
        public SymbolItemRangeList RangeList
        {
            get
            {
                if (rangeList == null)
                    rangeList = new SymbolItemRangeList();
                return rangeList;
            }

            set
            {
                rangeList = value;
            }

        }


    }

    [Serializable]
    public class SymbolItemRange
    {
        public object Start { get; set; }
        public object Finish { get; set; }
        public SymbolType Symbol { get; set; }
    }

    [Serializable]
    public class SymbolItemRangeList 
    {
        public DataType RangeDataType { get; set; }

        public SymbolItemRange this[int index]
        {
            get
            {
                return List[index];
            }

            set
            {
                List[index] = value;
            }
        }

        public void Clear()
        {
            List.Clear();
        }

        public void Add(SymbolItemRange item)
        {
            List.Add(item);
        }

        public List<SymbolItemRange> List
        {
            get
            {
                if (list == null)
                    list = new List<SymbolItemRange>();
                return list;
            }

            set
            {
                list = value;
            }
        }

        public void RemoveAt(int index)
        {
            List.RemoveAt(index);
        }

        public int Count
        {
            get
            {
                return List.Count;
            }
        }


        private List<SymbolItemRange> list = null;

        public void ConvertRanges()
        {
            SymbolItemRange xtemp = null; ;
            try
            {
                foreach (SymbolItemRange xitem in List)
                {
                    xtemp = xitem;
                    switch (RangeDataType)
                    {
                        case DataType.Integer:
                            {
                                xitem.Start = Convert.ToInt32(xitem.Start);
                                xitem.Finish = Convert.ToInt32(xitem.Finish);
                                break;
                            }
                        case DataType.DateTime:
                            {
                                xitem.Start = Convert.ToDateTime(xitem.Start);
                                xitem.Finish = Convert.ToDateTime(xitem.Finish);
                                break;
                            }
                        case DataType.Decimal:
                            {
                                xitem.Start = Convert.ToDecimal(xitem.Start);
                                xitem.Finish = Convert.ToDecimal(xitem.Finish);
                                break;
                            }
                        case DataType.Double:
                            {
                                xitem.Start = Convert.ToDouble(xitem.Start);
                                xitem.Finish = Convert.ToDouble(xitem.Finish);
                                break;
                            }
                        case DataType.Int64:
                            {
                                xitem.Start = Convert.ToInt64(xitem.Start);
                                xitem.Finish = Convert.ToInt64(xitem.Finish);
                                break;
                            }
                        case DataType.String:
                            {
                                xitem.Start = Convert.ToString(xitem.Start);
                                xitem.Finish = Convert.ToString(xitem.Finish);
                                break;
                            }
                    }
                }
            }
            catch
            {
                Exception ee = new Exception("Range \"" + xtemp.Start + " - " + xtemp.Finish + "\" can't be converted to " + RangeDataType.ToString() + " type");
                throw ee;
            }
        }

        public bool InRange(object value, out SymbolItemRange xitemrange)
        {
            bool xresult = false;
            xitemrange = null;
            if (value == null)
                return xresult;
            else
            {
                if (value is IComparable)
                {
                    try
                    {
                        foreach (SymbolItemRange item in List)
                        {
                            int cRes1 = ((IComparable)value).CompareTo(item.Start);
                            int cRes2 = ((IComparable)value).CompareTo(item.Finish);
                            if (cRes1 >= 0 && cRes2 <= 0)
                            {
                                xresult = true;
                                xitemrange = item;
                                break;
                            }
                        }
                        return xresult;
                    }
                    catch
                    {
                        return xresult;
                    }
                }
                else return xresult;
            }
        }
    }

}
